<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题项序号" prop="dtxXh">
        <el-input
          v-model="queryParams.dtxXh"
          placeholder="答题项序号,ABCD序号值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题项内容" prop="dtxNr">
        <el-input
          v-model="queryParams.dtxNr"
          placeholder="答题项内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--  -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dtx:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dtx:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dtx:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dtx:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dtxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="答题项ID" align="center" prop="dtxId" />
      <el-table-column label="答题项序号" align="center" prop="dtxXh" />
      <el-table-column label="答题项内容" align="center" prop="dtxNr" />
      <el-table-column label="答题项描述" align="center" prop="dtxMs" />
      <el-table-column label="是否正确答案" align="center" prop="sfzqda">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_ruankao_yes_no" :value="scope.row.sfzqda"/>
        </template>
      </el-table-column>
      <el-table-column label="答案分析" align="center" prop="dafx" />
      <el-table-column label="试卷题目ID" align="center" prop="tmId" />
      <el-table-column label="启用状态" align="center" prop="dtxZt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dtx_status" :value="scope.row.dtxZt"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="录入人" align="center" prop="lrr" />
      <el-table-column label="录入时间" align="center" prop="lrsj" />
      <el-table-column label="更新人" align="center" prop="gxr" />
      <el-table-column label="更新时间" align="center" prop="gxsj" />
      <el-table-column label="是否删除" align="center" prop="sfsc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_ruankao_yes_no" :value="scope.row.sfsc"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dtx:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dtx:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改试卷_答题项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="答题项ID" prop="dtxIdNew">
          <el-input v-model="form.dtxIdNew" placeholder="请输入答题项ID(最好遵循ID规则,修改时本字段为空即可)" />
        </el-form-item>
        <el-form-item label="答题项序号" prop="dtxXh">
          <el-input v-model="form.dtxXh" placeholder="请输入答题项序号,ABCD序号值" />
        </el-form-item>
        <el-form-item label="答题项内容" prop="dtxNr">
          <el-input v-model="form.dtxNr" placeholder="请输入答题项内容content" />
        </el-form-item>
        <el-form-item label="答题项描述" prop="dtxMs">
          <el-input v-model="form.dtxMs" placeholder="请输入答题项描述describ" />
        </el-form-item>
        <el-form-item label="是否正确答案" prop="sfzqda">
          <!-- <el-input v-model="form.sfzqda" placeholder="请输入是否正确答案" /> -->
          <el-radio-group v-model="form.sfzqda">
             <el-radio label="0">否</el-radio>
             <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="答案分析" prop="dafx">
          <el-input v-model="form.dafx" placeholder="请输入答案分析(在第一个正确答案分析)" />
        </el-form-item>
        <el-form-item label="试卷题目ID" prop="tmId">
          <el-input v-model="form.tmId" placeholder="请输入试卷题目tmId" />
        </el-form-item>
        <el-form-item label="是否启用" prop="dtxZt">
          <!-- <el-input v-model="form.dtxZt" placeholder="请输入0:未启用，1已启用state" /> -->
          <el-select v-model="form.dtxZt" placeholder="是否启用">
            <el-option label="启用" value="1"></el-option>
            <el-option label="停用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="录入人" prop="lrr">
          <el-input v-model="form.lrr" placeholder="请输入录入人" />
        </el-form-item>
        <el-form-item label="录入时间" prop="lrsj">
          <el-input v-model="form.lrsj" placeholder="请输入录入时间" />
        </el-form-item>
        <el-form-item label="更新人" prop="gxr">
          <el-input v-model="form.gxr" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="更新时间" prop="gxsj">
          <el-input v-model="form.gxsj" placeholder="请输入更新时间" />
        </el-form-item>
        <el-form-item label="是否删除" prop="sfsc">
          <!-- <el-input v-model="form.sfsc" placeholder="请输入是否删除" /> -->
          <el-radio-group v-model="form.sfsc">
             <el-radio label="0">否</el-radio>
             <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDtx, getDtx, delDtx, addDtx, updateDtx } from "@/api/system/dtx";

export default {
  name: "Dtx",
  dicts: ['sys_ruankao_yes_no','sys_dtx_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试卷_答题项表格数据
      dtxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dtxXh: null,
        dtxNr: null,
        dtxMs: null,
        sfzqda: null,
        dafx: null,
        tmId: null,
        dtxZt: null,
        lrr: null,
        lrsj: null,
        gxr: null,
        gxsj: null,
        sfsc: null
      },
      // 表单参数
      form: {
        tmId: null
      },
      // 表单校验
      rules: {
        tmId: [
          { required: true, message: "试卷题目IDquestion_id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const tmxxId = this.$route.query.tmId;
    if(tmxxId != '' && tmxxId != undefined && tmxxId != null ){
        this.queryParams.tmId = tmxxId;
        this.form.tmId = tmxxId;
    }
    this.getList();
  },
  methods: {
    /** 查询试卷_答题项列表 */
    getList() {
      /* const tmxxId = this.$route.query.tmId;
      if(tmxxId != '' && tmxxId != undefined && tmxxId != null ){
          this.queryParams.tmId = tmxxId;
          this.form.tmId = tmxxId;
      }
      alert(this.form.tmId); */
      this.loading = true;
      listDtx(this.queryParams).then(response => {
        this.dtxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dtxId: null,
        dtxIdNew: null,
        dtxXh: null,
        dtxNr: null,
        dtxMs: null,
        sfzqda: null,
        dafx: null,
        tmId: null,
        dtxZt: null,
        lrr: null,
        lrsj: null,
        gxr: null,
        gxsj: null,
        sfsc: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dtxId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // 带过父页面的题目Id
      const tmxxId = this.queryParams.tmId;
      if(tmxxId != '' && tmxxId != undefined && tmxxId != null ){
          this.form.tmId = tmxxId;
          this.form.dtxIdNew = 'dtx_'+tmxxId+"";
      }
      // dtx_
      this.form.dtxZt ="1" ; //启用状态
      this.form.sfsc ="0" ;  //删除状态
      const nowDateTime = this.getNowDateTime();
      this.form.lrr ="admin";
      this.form.lrsj = nowDateTime;
      this.form.gxr ="admin";
      this.form.gxsj = nowDateTime;
      this.open = true;
      this.title = "添加答题项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dtxId = row.dtxId || this.ids
      getDtx(dtxId).then(response => {
        if(response.data.lrr == null || response.data.lrr == undefined || response.data.lrr == "" ){
          response.data.lrr = "admin";
        }
        if(response.data.lrsj == null || response.data.lrsj == undefined || response.data.lrsj == "" ){
          response.data.lrsj = this.getNowDateTime();
        }
        if(response.data.gxr == null || response.data.gxr == undefined || response.data.gxr == "" ){
          response.data.gxr = "admin";
        }
        if(response.data.gxsj == null || response.data.gxsj == undefined || response.data.gxsj == "" ){
          response.data.gxsj = this.getNowDateTime();
        }
        if(response.data.sfsc == null || response.data.sfsc == undefined || response.data.sfsc == "" ){
          response.data.sfsc = "0";
        }
        this.form = response.data;
        console.info(response.data.gxsj)
        this.open = true;
        this.title = "修改试卷_答题项";
      });
    },
    getNowDateTime(){
      var now = new Date();
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      if(month < 10 ){
        month = "0"+month;
      }
      var date = now.getDate();
      if(date < 10 ){
        date = "0"+date;
      }
      var hours = now.getHours();
      if(hours < 10 ){
        hours = "0"+hours;
      }
      var minutes = now.getMinutes();
      if(minutes < 10 ){
        minutes = "0"+minutes;
      }
      var seconds = now.getSeconds();
      if(seconds < 10 ){
        seconds = "0"+seconds;
      }
      return year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dtxId != null) {
            updateDtx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //新增时，自定义dtxId(使用自己按规则增加的ID,而后台不再设置dtxId)
            this.form.dtxId = this.form.dtxIdNew ;
            addDtx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dtxIds = row.dtxId || this.ids;
      this.$modal.confirm('是否确认删除试卷_答题项编号为"' + dtxIds + '"的数据项？').then(function() {
        return delDtx(dtxIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dtx/export', {
        ...this.queryParams
      }, `dtx_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
